package com.soft.idc.common.control;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;

import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.ICallback;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.soft.idc.common.model.IdcUser;
import com.soft.idc.common.util.JQPage;

public class AuthTemplateManagerController extends Controller {

	private static final Logger log = Logger.getLogger(AuthTemplateManagerController.class);

	public void index() {
		this.render("auth_template/auth_template.html");
	}

	public void queryAuthTemplate() {
		int start = this.getParaToInt("start");
		int length = this.getParaToInt("length");
		IdcUser u = this.getSessionAttr("SYS_USER");
		String auth_template_name = this.getPara("auth_template_name");
		String is_use = this.getPara("is_use");
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT T.AUTH_ID, T.NAME, IF(T.IS_USE=0, \"在用\", \"弃用\") AS IS_USE ");
		StringBuffer sqlFrom = new StringBuffer();
		sqlFrom.append(" FROM IDC_AUTH_TEMPLATE T WHERE 1=1 ");
		if (auth_template_name != null) {
			sqlFrom.append(" AND T.NAME LIKE '%" + auth_template_name + "%' ");
		}

		if (is_use != null) {
			int isUse = this.getParaToInt("is_use");
			sqlFrom.append(" and T.IS_USE = " + isUse + " ");
		}
		int pageNum = start / length + 1;

		Page<Record> page = Db.paginate(pageNum, length, sb.toString(), sqlFrom.toString());

		JQPage jPage = new JQPage(page);

		this.renderJson(jPage);
	}

	public void add() {
		String authTemplateName = this.getPara("authTemplateName");
		String isUse = this.getPara("isUse");
		String sql = "INSERT INTO IDC_AUTH_TEMPLATE (NAME, IS_USE) VALUES (? , ? )";
		int result = Db.update(sql, new Object[] { authTemplateName, isUse });
		this.renderJson(result);
	}

	public void delete() {
		final String authIds = this.getPara("authIds");
		ICallback call = new ICallback() {

			@Override
			public Object call(Connection conn) throws SQLException {
				// TODO Auto-generated method stub
				conn.setAutoCommit(false);
				String[] authIdArr = authIds.split(";");
				StringBuffer sb = new StringBuffer();
				sb.append("DELETE FROM IDC_AUTH_TEMPLATE WHERE AUTH_ID IN(");
				for (int i = 0; i < authIdArr.length; i++) {
					if (i == authIdArr.length - 1) {
						sb.append(authIdArr[i] + ")");
					} else {
						sb.append(authIdArr[i] + ",");
					}
				}
				Statement sta = conn.createStatement();
				int count = sta.executeUpdate(sb.toString());
				if (count == authIdArr.length) {
					conn.commit();
					conn.close();
					return true;
				} else {
					conn.rollback();
					conn.close();
					return false;
				}
			}
		};
		Object obj = Db.execute(call);
		this.renderJson(obj);
	}

	public void edit() {
		String auth_id = this.getPara("auth_id");
		String auth_name = this.getPara("auth_name");
		String is_use = this.getPara("is_use");
		String sql = "UPDATE IDC_AUTH_TEMPLATE SET NAME=? , IS_USE=?  WHERE AUTH_ID=? ";
		int result = Db.update(sql, new Object[] { auth_name, is_use, auth_id });
		this.renderJson(result);
	}
}
